<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-row>
          <el-tree :data="list" :default-expand-all="true">
            <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
            <!--
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (table 单元格数据渲染)
    -->
            <template #default="{ data }">
              <el-row
                type="flex"
                justify="space-between"
                align="middle"
                style="height: 40px; width: 100%"
              >
                <el-col :span="20">
                  <span>{{ data.name }}</span>
                </el-col>
                <el-col :span="4">
                  <el-row type="flex" justify="end">
                    <!-- 两个内容 -->
                    <el-col>{{ data.manager }}</el-col>
                    <el-col>
                      <!-- 下拉菜单 element -->
                      <el-dropdown>
                        <span> 操作<i class="el-icon-arrow-down" /> </span>
                        <!-- 下拉菜单 -->
                        <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item @click.native="showDailog(data,'add')">添加子部门</el-dropdown-item>
                          <el-dropdown-item @click.native="showDailog(data)">编辑部门</el-dropdown-item>
                          <el-dropdown-item
                            @click.native="deleteitem(data)"
                          >删除部门</el-dropdown-item>
                        </el-dropdown-menu>
                      </el-dropdown>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </template>
          </el-tree>
        </el-row>
      </el-card>
    </div>
    <dailogForm v-if="dialogFormVisible" :all-code="allDepartmentCode" :itemdata="itemData" :dailog-show="dialogFormVisible" @changeState="change" />
  </div>
</template>

<script>
import dailogForm from './daiLogeForm'
import { getDepartmentList, deleteDepartment, editDepartment, addDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
export default {
  components: {
    dailogForm
  },
  data() {
    return {
      list: [],
      dialogFormVisible: false,
      itemData: {},
      allDepartmentCode: [] // 所有部门的编码

    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    async getList() {
      const res = await getDepartmentList()
      this.allDepartmentCode = res.data.data.depts.map(item => item.code)
      console.log(res.data.data.depts)
      this.list = tranListToTreeData(res.data.data.depts)
    },
    deleteitem(item) {
      console.log(item)
      this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await deleteDepartment(item.id)
        console.log(res)
        this.$message({
          message: '删除成功',
          type: 'success'
        })

        this.getList()
      }).catch(() => {
        this.$message('已取消操作')
      })
    },
    showDailog(data, str) {
      console.log(data)
      if (str) { // 点击新增
        console.log('点击了', data)
        this.itemData = { pid: data.id }
        this.itemData.add = true
      } else { // 点击编辑
        this.itemData = data
      }
      console.log('dakai')
      this.dialogFormVisible = true
    },
    async change(fID, form, str) {
      this.dialogFormVisible = false
      console.log('str', str)
      if (fID && form) {
        if (str) {
          console.log('新增')
          const obj = form
          obj.pid = fID
          const res = await addDepartment(obj)
          console.log(res)
          this.$message.success(res.data.message)
        } else {
          const res = await editDepartment(fID, form)
          console.log('编辑', res)
          this.$message.success(res.data.message)
        }
        this.getList()
      }
    }
  }
}
</script>

<style scoped>

</style>
